﻿using LiveCharts;
using LiveCharts.Configurations;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using ZYing.QPanel.Constants;

namespace ZYing.QPanel.Views
{
    /// <summary>
    /// Scheduler.xaml 的交互逻辑
    /// </summary>
    public partial class Scheduler : UserControl
    {
        public Scheduler()
        {
            InitializeComponent();
            //let create a mapper so LiveCharts know how to plot our CustomerViewModel class
            var customerVmMapper = Mappers.Xy<ExecutionHistoryEntry>()
                .X((value, index) => index) // lets use the position of the item as X
                .Y(value => value.GetRunTime().TotalSeconds)//and PurchasedItems property as Y
                .Fill(value =>
                {
                    if (value.FinishedTimeUtc == null) return BrushConstants.Running;
                    if (string.IsNullOrWhiteSpace(value.ExceptionMessage)) return BrushConstants.Complated;
                    else return BrushConstants.Fail;
                })
                .Stroke(x => Brushes.Black);

            //lets save the mapper globally
            Charting.For<ExecutionHistoryEntry>(customerVmMapper);
            HistoryAxisY.LabelFormatter = HistoryAxisY_LabelFormatter;
        }
        private static string HistoryAxisY_LabelFormatter(double value)
        {
            var span = TimeSpan.FromSeconds(value);
            if (span.Seconds < 1)
            {
                return span.ToString("mm\\:ss\\.fff");
            }
            return span.ToString("hh\\:mm\\:ss");
        }
        private string LabelPoint(ChartPoint arg)
        {
            if (arg.Instance is ExecutionHistoryEntry e)
            {
                return e.Job.ToString();
            }
            return arg.XStart.ToString();
        }
    }
}
